package com.link.play.fit.ai.utils

import com.link.play.fit.ai.R
import java.math.BigDecimal
import java.math.RoundingMode

/**
 *Create by SleepDog on 2025-04-03
 */
object BmiUtils {

    private val levelList by lazy(LazyThreadSafetyMode.NONE) {
        AppX.context.run {
            listOf(
                getString(R.string.underweight), getString(R.string.healthy),
                getString(R.string.overweight), getString(R.string.obese)
            )
        }
    }

    private val colorList by lazy(LazyThreadSafetyMode.NONE) {
        AppX.context.run {
            listOf(
                getColor(R.color.color_007BFF), getColor(R.color.color_34C85A),
                getColor(R.color.color_FFD60A), getColor(R.color.color_FF3B2F)
            )
        }
    }


    fun getLevel(bmi: Float): Int {
        return if (bmi <= 18.4f) 0 else if (bmi in 18.5f..24.9f) 1 else if (bmi in 25f..29.9f) 2 else 3
    }

    fun getData(level: Int) = levelList[level] to colorList[level]


    /**
     * 获得bmi
     * @param weightKg 体重kg
     * @param height 身高 cm
     * @return bmi
     */
    fun getBmi(weightKg: Int, height: Int): Float {
        if(weightKg==0||height==0)return 0f
        val heightM = height.toFloat() / 100f
        val result = (weightKg.toFloat() / (heightM * heightM))
        return BigDecimal(result.toDouble()).setScale(1, RoundingMode.HALF_UP).toFloat()
    }
}